<?php
namespace Admin\Controller;

/**
 * 页面管理
 * Class PageController
 * @package Admin\Controller
 */
class PageController extends BaseController {

    /**
     * 马上买
     */
    public function index(){

        $name = I('name', '', htmlspecialchars());
        $local_id = session('userInfo')['local_id'];

        if (!empty($name)) {
            $where['b.name'] = array('like',"%$name%");
        }

        $page = max(I('get.p'), 1);
        $num = max(I('get.num'), 20);

        $order = "a.store_pid desc";
        $product_relation_model = M('b2c_store_product a');

        $where['a.local_id'] = session('userInfo')['local_id'];
        $where['a.store_status'] = 'true';

        $field = "a.marketprice,a.price,a.store,a.discount,a.store_pid,b.product_id,b.goods_id,b.title,b.name,b.spec_info";

        // 可售货品
        $count = $product_relation_model->join(C('DB_PREFIX') . 'b2c_products b on a.product_id=b.product_id')
            ->where($where)
            ->count();

        $Page = new \Think\Page($count, $num);
        $show = $Page->show();
        $product_relation_msg = $product_relation_model->join(C('DB_PREFIX') . 'b2c_products b on a.product_id=b.product_id', 'left')
            ->where($where)
            ->order($order)
            ->field($field)
            ->page($page, $num)
            ->select();

        $menu = M('store_page')->where(['local_id' => $local_id, 'type' => '马上买'])->find();
        $value = explode(',', $menu['value']);

        $this->assign([
            'product_relation_msg' => $product_relation_msg,
            'show' => $show,
            'value' => $value
        ]);
        $this->display();
    }

    /**
     * 买上买保存
     */
    public function maShangMai(){
        $m = M('store_page');
        $local_id = session('userInfo')['local_id'];

        if ($_POST) {
            get_curl(session('userInfo')['local_id']);
            $data = I('data', '', htmlspecialchars());
            $value = [];
            $menu = $m->where(['local_id' => $local_id, 'type' => '马上买'])->find();
            if (empty($menu)) {
                $m->add(['local_id' => $local_id, 'type' => '马上买']);
            }
            if (!empty($menu['value'])) { // 如果value有值
                $value = explode(',', $menu['value']);
            }
            if (!empty($data)) {
                foreach ($data as $v) {
                    $arr = explode('-', $v);

                    if ($arr[1] == 0) { // 第二个元素为0删除
                        $key = array_search($arr[0], $value);
                        if ($key !== false) {
                            array_splice($value, $key, 1);
                        }
                    }else {
                        $key = array_search($arr[0], $value);
                        if ($key === false) {
                            array_push($value,$arr[0]);
                        }
                    }
                }
                $value = implode(',', $value);
                $m->where(['local_id' => $local_id, 'type' => '马上买'])->save(['value' => $value]);
            }
        }
    }

    /**
     * 推荐
     */
    public function recommend(){
        $name = I('name', '', htmlspecialchars());
        $local_id = session('userInfo')['local_id'];

        if (!empty($name)) {
            $where['b.name'] = array('like',"%$name%");
        }

        $page = max(I('get.p'), 1);
        $num = max(I('get.num'), 20);

        $order = "a.store_pid desc";
        $product_relation_model = M('b2c_store_product a');

        $where['a.local_id'] = session('userInfo')['local_id'];
        $where['a.store_status'] = 'true';
        $where['b.disabled'] = 'false';

        $field = "a.marketprice,a.price,a.store,a.discount,a.store_pid,b.product_id,b.goods_id,b.title,b.name,b.spec_info";

        // 可售货品
        $count = $product_relation_model->join(C('DB_PREFIX') . 'b2c_products b on a.product_id=b.product_id')
            ->where($where)
            ->count();

        $Page = new \Think\Page($count, $num);
        $show = $Page->show();
        $product_relation_msg = $product_relation_model->join(C('DB_PREFIX') . 'b2c_products b on a.product_id=b.product_id', 'left')
            ->where($where)
            ->order($order)
            ->field($field)
            ->page($page, $num)
            ->select();

        $menu = M('store_page')->where(['local_id' => $local_id, 'type' => '推荐'])->find();
        $value = explode(',', $menu['value']);

        $this->assign([
            'product_relation_msg' => $product_relation_msg,
            'show' => $show,
            'value' => $value
        ]);
        $this->display();
    }

    /**
     * 推荐保存
     */
    public function tuijian(){
        $m = M('store_page');
        $local_id = session('userInfo')['local_id'];

        if ($_POST) {
            get_curl(session('userInfo')['local_id']);
            $data = I('data', '', htmlspecialchars());
            $value = [];
            $menu = $m->where(['local_id' => $local_id, 'type' => '推荐'])->find();
            if (empty($menu)) {
                $m->add(['local_id' => $local_id, 'type' => '推荐']);
            }
            if (!empty($menu['value'])) { // 如果value有值
                $value = explode(',', $menu['value']);
            }
            if (!empty($data)) {
                foreach ($data as $v) {
                    $arr = explode('-', $v);

                    if ($arr[1] == 0) { // 第二个元素为0删除
                        $key = array_search($arr[0], $value);
                        if ($key !== false) {
                            array_splice($value, $key, 1);
                        }
                    }else {
                        $key = array_search($arr[0], $value);
                        if ($key === false) {
                            array_push($value,$arr[0]);
                        }
                    }
                }
                $value = implode(',', $value);
                $m->where(['local_id' => $local_id, 'type' => '推荐'])->save(['value' => $value]);
            }
        }
    }


}